<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="120px" :inline="true">
            <el-form-item :label="$t('status1')">
              <el-select v-model="tableFrom.status" :placeholder="$t('pleaseselect')" class="filter-item selWidth mr20"
                clearable @change="getList(1)">
                <el-option :label="$t('notopen')" :value="0" />
                <el-option :label="$t('open')" :value="1" />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('couponname')">
              <el-input v-model="tableFrom.coupon_name" :placeholder="$t('plecouponname')" class="selWidth mr20"
                clearable @keyup.enter.native="getList(1)">
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getList(1)" />
              </el-input>
            </el-form-item>
            <el-form-item :label="$t('coupontype')">
              <el-select v-model="tableFrom.type" :placeholder="$t('pleaseselect')" class="filter-item selWidth mr20"
                clearable @change="getList(1)">
                <el-option :label="$t('all')" value="" />
                <el-option :label="$t('shopcoupon')" :value="0" />
                <el-option :label="$t('commoditycoupon')" :value="1" />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('acquisitionmethod')">
              <el-select v-model="tableFrom.send_type" :placeholder="$t('pleaseselect')"
                class="filter-item selWidth mr20" clearable @change="getList(1)">
                <el-option :label="$t('all')" value="" />
                <el-option :label="$t('manualacquisition')" :value="0" />
                <el-option :label="$t('newpeople')" :value="2" />
                <el-option :label="$t('buyfree')" :value="3" />
              </el-select>
            </el-form-item>
          </el-form>
        </div>

        <router-link :to="{ path: `${roterPre}` + '/marketing/coupon/creatCoupon' }">
          <el-button size="small" type="primary">{{
            $t("addcoupon")
          }}</el-button>
        </router-link>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="small" highlight-current-row>
        <el-table-column prop="coupon_id" label="ID" min-width="50" />
        <el-table-column prop="title" :label="$t('nameofcoupon')" min-width="120" />
        <el-table-column :label="$t('typeofcoupon')" min-width="90">
          <template slot-scope="{ row }">
            <span>{{ row.type | couponTypeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="200" :label="$t('dateofreceipt')">
          <template slot-scope="{ row }">
            <div v-if="row.start_time">
              {{ row.start_time }} <br />- {{ row.end_time }}
            </div>
            <span v-else>{{ $t("unlimitedtime") }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="200" :label="$t('usagetime')">
          <template slot-scope="{ row }">
            <div v-if="row.use_start_time && row.use_end_time">
              {{ row.use_start_time }} <br />- {{ row.use_end_time }}
            </div>
            <span v-else>{{ row.coupon_time }}{{ $t("day") }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="100" :label="$t('numberofreleases')">
          <template slot-scope="{ row }">
            <span v-if="row.is_limited === 0">{{ $t("unlimited") }}</span>
            <div v-else>
              <span class="fa">发布：{{ row.total_count }}</span>
              <span class="sheng">剩余：{{ row.remain_count }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column min-width="100" :label="$t('quantityused')">
          <template slot-scope="{ row }">
            <div>
              <span>{{ $t("totalreceiveddistributed") }}：{{ row.send_num }}</span>
              <span class="sheng">{{ $t("totalused") }}：{{ row.used_num }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('state')" min-width="100">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" :active-text="$t('display')"
              :inactive-text="$t('hide')" @click.native="onchangeIsShow(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('operation')" min-width="150" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" class="mr10" @click="details(scope.row.coupon_id)">{{
              $t("details")
            }}</el-button>
            <el-button type="text" size="small" class="mr10" @click="receive(scope.row.coupon_id)">
              {{ $t("receivingdistrib") }}</el-button>
            <el-button type="text" size="small" class="mr10" @click="onEdit(scope.row.coupon_id)">{{
              $t("edit")
            }}</el-button>
            <router-link :to="{
              path:
                `${roterPre}` +
                '/marketing/coupon/creatCoupon/' +
                scope.row.coupon_id,
            }">
              <el-button type="text" size="small" class="mr10">{{
                $t("copy")
              }}</el-button>
            </router-link>
            <el-button type="text" size="small" @click="handleDelete(scope.row.coupon_id, scope.$index)">{{
              $t("delete")
            }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
          @current-change="pageChange" />
      </div>
    </el-card>
    <!--优惠券详情-->
    <el-dialog v-if="detailDialog" :title="$t('coupondetails')" :visible.sync="detailDialog" width="700px">
      <div>
        <div class="box-container">
          <div class="list sp">
            <label class="name">{{ $t("couponname") }}</label>
            <span class="info">{{ couponDetail.title }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("coupontype") }}：</label>
            <span class="info">{{ couponDetail.type | couponTypeFilter }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("couponfacevalue") }}：</label>
            <span class="info">{{ couponDetail.coupon_price }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("usethreshold") }}：</label>
            <span class="info">{{
              couponDetail.use_min_price == "0.00"
                ? $t("nothreshold")
                : $t("minimumconsumption") + couponDetail.use_min_price
            }}</span>
          </div>
          <div class="list sp100">
            <label class="name">{{ $t("validperiod") }}</label>
            <span v-if="couponDetail.coupon_time && couponDetail.coupon_type == 0" class="info">{{
              couponDetail.coupon_time
            }}{{ $t("day") }}</span>
            <span v-else-if="couponDetail.coupon_type == 1" class="info">{{
              couponDetail.use_start_time + " - " + couponDetail.use_end_time
            }}</span>
          </div>
          <div class="list sp100">
            <label class="name">{{ $t("collectiontime") }}：</label>
            <span v-if="couponDetail.is_timeout == 1" class="info">{{ couponDetail.start_time }} - {{
              couponDetail.end_time
            }}</span>
            <span v-else class="info">{{ $t("unlimitedtime") }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("type") }}：</label>
            <span class="info">{{
              couponDetail.send_type | couponUseTypeFilter
            }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("limitedornot") }}：</label>
            <span class="info">{{
              couponDetail.is_limited | filterClose
            }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("totalpublished") }}：</label>
            <span class="info">{{
              couponDetail.is_limited == 0
                ? $t("unlimited")
                : couponDetail.total_count
            }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("totalremaining") }}：</label>
            <span class="info">{{
              couponDetail.is_limited == 0
                ? $t("unlimited")
                : couponDetail.remain_count
            }}</span>
          </div>
          <div class="list sp100">
            <label class="name">{{ $t("totalreceiveddistributed") }}：</label>
            <span class="info">{{ couponDetail.send_num }}</span>
            <el-button size="small" type="text" class="ml20" @click="receive(couponDetail.coupon_id)">{{
              $t("receivingdistrib")
            }}</el-button>
          </div>
          <div class="list sp100">
            <label class="name">{{ $t("totalused") }}：</label>
            <span class="info">{{ couponDetail.used_num }}</span>
            <el-button size="small" type="text" class="ml20" @click="usedRecord(couponDetail.coupon_id)">{{
              $t("usagerecord")
            }}</el-button>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("sort") }}：</label>
            <span class="info">{{ couponDetail.sort }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("state") }}：</label>
            <span class="info">{{
              couponDetail.status ? $t("open") : $t("close")
            }}</span>
          </div>
          <div v-if="type == 1" class="list sp100">
            <el-table v-loading="listLoading" :data="relateData.data">
              <el-table-column prop="product.product_id" label="ID" min-width="50" />
              <el-table-column :label="$t('commoditymap')" min-width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.product.image" class="demo-image__preview">
                    <img style="width: 36px; height: 36px" :src="scope.row.product.image" />
                  </div>
                  <div v-else class="demo-image__preview">
                    <img style="width: 36px; height: 36px" src="../../../../assets/images/f.png" />
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="product.store_name" :label="$t('tradename')" min-width="150" />
              <el-table-column prop="product.stock" :label="$t('stock')" min-width="50" />
              <el-table-column prop="product.price" :label="$t('sellingprice')" min-width="50" />
              <el-table-column prop="product.sales" :label="$t('salesvolumes')" min-width="50" />
            </el-table>
            <div class="block mb20">
              <el-pagination :page-sizes="[5, 10]" :page-size="tableFromRelate.limit"
                :current-page="tableFromRelate.page" layout="total, sizes, prev, pager, next, jumper"
                :total="relateData.total" @size-change="handleSizeChangeRelate" @current-change="pageChangeRelate" />
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
    <!--领取记录-->
    <el-dialog :title="title" :visible.sync="dialogVisible" min-width="500px" :before-close="handleClose"
      class="modalbox">
      <el-table v-loading="Loading" :data="issueData.data" style="width: 100%" size="small" highlight-current-row>
        <el-table-column prop="user.nickname" :label="$t('username')" min-width="120" />
        <el-table-column :label="$t('useravatar')" min-width="80">
          <template slot-scope="scope">
            <div v-if="scope.row.user.avatar" class="demo-image__preview">
              <img style="width: 36px; height: 36px" :src="scope.row.user.avatar" />
            </div>
            <div v-else class="demo-image__preview">
              <img style="width: 36px; height: 36px" src="../../../../assets/images/f.png" />
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="receiveTime" min-width="180">
          <template slot-scope="scope">
            <span v-if="receiveType === 0">{{ scope.row.create_time }}</span>
            <span v-else>{{ scope.row.use_time }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[10, 20, 30, 40]" :page-size="tableFromIssue.limit"
          :current-page="tableFromIssue.page" layout="total, sizes, prev, pager, next, jumper" :total="issueData.total"
          @size-change="handleSizeChangeIssue" @current-change="pageChangeIssue" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  couponIssueListApi,
  couponIssueStatusApi,
  issueApi,
  couponDeleteApi,
  couponDetailApi,
  couponRelateProLst,
  couponUpdateApi,
} from "@/api/marketing";
import { roterPre } from "@/settings";
export default {
  name: "CouponList",
  data() {
    return {
      Loading: false,
      dialogVisible: false,
      detailDialog: false,
      roterPre: roterPre,
      listLoading: true,
      title: this.$t("receivingdistrib"),
      receiveTime: this.$t("collectiontime"),
      receiveType: 0,
      id: "",
      tableData: {
        data: [],
        total: 0,
      },
      tableFrom: {
        page: 1,
        limit: 20,
        status: "",
        coupon_name: "",
        type: "",
        send_type: "",
      },
      tableFromIssue: {
        page: 1,
        limit: 10,
        coupon_id: 0,
      },
      issueData: {
        data: [],
        total: 0,
      },
      relateData: {
        data: [],
        total: 0,
      },
      tableFromRelate: {
        page: 1,
        limit: 5,
      },
      couponDetail: {},
      type: 0,
    };
  },
  mounted() {
    this.getList(1);
  },
  methods: {
    // 删除
    handleDelete(id, idx) {
      this.$modalSure().then(() => {
        couponDeleteApi(id)
          .then(({ message }) => {
            this.$message.success(message);
            this.tableData.data.splice(idx, 1);
          })
          .catch(({ message }) => {
            this.$message.error(message);
          });
      });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    // 详情
    details(id) {
      this.detailDialog = true;
      this.type = 0;
      couponDetailApi(id)
        .then((res) => {
          this.couponDetail = res.data;
          this.type = res.data.type;
          this.id = id;
          if (res.data.type == 1) this.getRelateList(id);
        })
        .catch(({ message }) => {
          this.$message.error(message);
        });
    },
    // 编辑
    onEdit(id) {
      this.$modalForm(couponUpdateApi(id)).then(() => this.getList(""));
    },
    // 领取记录
    receive(id) {
      this.dialogVisible = true;
      this.title = this.$t("receivingdistrib");
      this.receiveTime = this.$t("collectiontime");
      this.receiveType = 0;
      this.tableFromIssue.coupon_id = id;
      this.getIssueList("");
    },
    // 使用记录
    usedRecord(id) {
      this.dialogVisible = true;
      this.title = this.$t("usagerecord");
      this.receiveTime = this.$t("usagetime");
      this.receiveType = 1;
      this.tableFromIssue.coupon_id = id;
      this.getIssueList(1);
    },
    // 列表
    getIssueList(status) {
      this.Loading = true;
      this.tableFromIssue.status = status;
      issueApi(this.tableFromIssue)
        .then((res) => {
          this.issueData.data = res.data.list;
          this.issueData.total = res.data.count;
          this.Loading = false;
        })
        .catch((res) => {
          this.Loading = false;
          this.$message.error(res.message);
        });
    },
    pageChangeIssue(page) {
      this.tableFromIssue.page = page;
      let status = this.receiveType == 1 ? 1 : "";
      this.getIssueList(status);
    },
    handleSizeChangeIssue(val) {
      this.tableFromIssue.limit = val;
      let status = this.receiveType == 1 ? 1 : "";
      this.getIssueList(status);
    },
    // 列表
    getRelateList(id) {
      this.Loading = true;
      this.relateData.data = [];
      couponRelateProLst(id, this.tableFromRelate)
        .then((res) => {
          this.relateData.data = res.data.list;
          this.relateData.total = res.data.count;
          this.Loading = false;
        })
        .catch((res) => {
          this.Loading = false;
          this.$message.error(res.message);
        });
    },
    pageChangeRelate(page) {
      this.tableFromRelate.page = page;
      this.getRelateList(this.id);
    },
    handleSizeChangeRelate(val) {
      this.tableFromRelate.limit = val;
      this.getRelateList(this.id);
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num || this.tableFrom.page;
      couponIssueListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList("");
    },
    // 修改状态
    onchangeIsShow(row) {
      couponIssueStatusApi(row.coupon_id, row.status)
        .then(({ message }) => {
          this.$message.success(message);
          this.getList("");
        })
        .catch(({ message }) => {
          this.$message.error(message);
        });
    },
  },
};
</script>

<style scoped lang="scss">
.modalbox {
  ::v-deep.el-dialog {
    min-width: 550px;
  }
}

.selWidth {
  width: 260px !important;
}

.seachTiele {
  line-height: 35px;
}

.fa {
  color: #0a6aa1;
  display: block;
}

.sheng {
  color: #ff0000;
  display: block;
}

.ml20 {
  margin-left: 20px;
}

.box-container {
  overflow: hidden;
}

.box-container .list {
  float: left;
  line-height: 40px;
}

.box-container .sp {
  width: 50%;
}

.box-container .sp3 {
  width: 33.3333%;
}

.box-container .sp100 {
  width: 100%;
}

.box-container .list .name {
  display: inline-block;
  width: 150px;
  text-align: right;
  color: #606266;
}

.box-container .list .blue {
  color: #1890ff;
}

.box-container .list.image {
  margin-bottom: 40px;
}

.box-container .list.image img {
  position: relative;
  top: 40px;
}
</style>
